package cn.lingxue.lingapi.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import cn.lingxue.lingapi.entity.Login;
import cn.lingxue.lingapi.service.LoginService;
import cn.lingxue.lingapi.utils.ResponseResult;
import cn.lingxue.lingapi.utils.SMSUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@CrossOrigin("*")
@RequestMapping("/api/auth")
@RestController
public class IndexController {
    @Resource
    LoginService loginService;

    @Resource
    SMSUtil smsUtil;
    /**
     * 系统用户登录
     *img_1.png
     * @param userName 账户账号
     * @param password 账户密码
     * @return cn.duozai.ct.utils.response.ResponseResult
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestParam(name = "userName") String userName,
                                @RequestParam(name = "password") String password) {
        // 根据账户账号查询系统用户信息
        Login login = loginService.getOne(new QueryWrapper<Login>().eq("userName",userName));

        // 判断账户账号是否存在
        if (login == null){
            return ResponseResult.failure("账户不存在！");
        }

        // 判断密码是否正确
        String md5SaltsPassword = password;
        if(!md5SaltsPassword.equals(login.getPassword())) {
            return ResponseResult.failure("账户密码错误！");
        }


        // 登录成功，调用StpUtil进行登录get
        StpUtil.login(login.getId());

        // 返回成功结果 + token令牌
        return ResponseResult.success("登录成功", MapUtil.builder().put("token", StpUtil.getTokenValue()).build());
    }

    /**
     * 系统用户注销
     *
     */
    @GetMapping("/logout")
    public ResponseResult logout() {
        StpUtil.logout();
        return ResponseResult.success("注销成功");
    }

    /**
     * 获取用户信息
     *
     */
    @GetMapping("/getUserInfo")
    public ResponseResult<Login> getUserInfo() {
        if (!StpUtil.isLogin()) {
            return ResponseResult.failure("用户未登录");
        }
        Login loginServiceOne = loginService.getOne(new QueryWrapper<Login>().eq("id", StpUtil.getLoginIdAsLong()));
        return ResponseResult.success(loginServiceOne);
    }

    /**
     * 发送验证码
     *
     */
    @PostMapping("/sendSms")
    public ResponseResult sendSms(@RequestParam(name = "phone") String phone) {
        String s = smsUtil.sendSms(phone);
        if (Strings.isNotBlank(s)){
            return ResponseResult.success("发送验证码成功", s);
        }
        return ResponseResult.failure("发送验证码失败");
    }

    /**
     * 注册
     *
     */
    @PostMapping("/register")
    public ResponseResult register(@RequestParam(name = "userName") String userName,
                                  @RequestParam(name = "password") String password,
                                  @RequestParam(name = "phone") String phone,
                                  @RequestParam(name = "code") String code) {
        boolean result = smsUtil.validSms(phone, code);
        if (!result){
            return ResponseResult.failure("验证码错误");
        }
        if (loginService.getOne(new QueryWrapper<Login>().eq("userName",userName)) != null){
            return ResponseResult.failure("账户已存在");
        }
        Login login = new Login();
        login.setUsername(userName);
        login.setPassword(password);
        login.setPhone(phone);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        login.setCreateTime(simpleDateFormat.format(new Date()));
        boolean save = loginService.save(login);
        if (!save){
            return ResponseResult.failure("注册失败");
        }
        return ResponseResult.success("注册成功");
    }


}
